package com.tzy.common.utils;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

public class MessageUtil {

    /**
     * 存放多语言资源
     */
    private static Map<Locale, ResourceBundle> bundles = new HashMap<>();

    /**
     * 从语言包中获取code对应的信息
     * @param code
     * @param lang
     * @return
     */
    public static String getMsg(int code, Locale lang){
        ResourceBundle resourceBundle = bundles.get(lang);
        if(resourceBundle == null){
            resourceBundle = ResourceBundle.getBundle("message", lang);
            bundles.put(lang,resourceBundle);
        }
        return resourceBundle.getString(String.valueOf(code));
    }

    public static String getMsg(int code){
        Locale lang = null;
        if(lang == null){
            lang = Locale.CHINA;
        }
        return getMsg(code,lang);
    }

}
